package com.yunbiao.ybbusstopplate.ui.view;

import android.content.Context;
import android.os.Handler;
import android.widget.FrameLayout;
import com.yunbiao.ybbusstopplate.ui.view.subway.GlobalSetting;
import com.yunbiao.ybbusstopplate.ui.view.subway.StationEffect;
import com.yunbiao.ybbusstopplate.ui.view.subway.StationMap3;

import java.util.ArrayList;
import java.util.List;

public class SubwayLooper2 extends SubwayLooper {
    private int maxCount=50;
    private List<String> datas;
    private List<List<String[]>> pages;
    private Handler playHandler;
    private PlayRunable playRunable;
    private boolean isLooper=false;
    private int index=0;
    private Context context;
    private FrameLayout fl_base;
    private FrameLayout fl_top;
    private FrameLayout fl_bottom;
    private long playTime=1000*20;
    private List<String[]> showDatas;

    public SubwayLooper2(Context context, FrameLayout fl_base){
        this.context=context;
        this.fl_base=fl_base;
    }

    public SubwayLooper2(Context context, FrameLayout fl_base, int maxCount){
        this.context=context;
        this.fl_base=fl_base;
        this.maxCount=maxCount;
    }

    public void setDatas(List<String> datas){
        this.datas=datas;
        showDatas=new ArrayList<>();
        for (int i = 0; i < datas.size(); i++) {
            showDatas.add(new String[]{i+"",datas.get(i)});
        }
        anlzyData();
    }

    public void setDatas(List<String> datas,int maxCount){
        this.datas=datas;
        this.maxCount=maxCount;
        showDatas=new ArrayList<>();
        for (int i = 0; i < datas.size(); i++) {
            showDatas.add(new String[]{i+"",datas.get(i)});
        }
        anlzyData();
    }

    public void desty(){
        if (playHandler!=null&&playRunable!=null){
            playHandler.removeCallbacks(playRunable);
        }
        isLooper=false;
        if (effect!=null){
            effect.threadstop();
        }
    }

    private void anlzyData(){
        desty();
        int size = showDatas.size();

        updateSubView(showDatas,maxCount);

//        if (size<=maxCount){
//            //单屏显示
//            updateSubView(showDatas,maxCount);
//        }else {
//            int page=1;//页数
//            int tempCount=maxCount;
//            while (size>tempCount){
//                page+=1;
//                tempCount+=maxCount;
//            }
//            int count4onepage =(int)Math.ceil((double) size / page);
//            pages=new ArrayList<>();
//            //递归处理数据
//            slipPageData(showDatas,count4onepage);
////            int size1 = pages.size();
//            isLooper=true;
//            playHandler=new Handler();
//            playRunable=new PlayRunable();
//            playHandler.postDelayed(playRunable,50);
//        }
    }

    private StationEffect effect;
    private StationMap3 stationMap;
    private void updateSubView(List<String[]> datas,int limit){
        fl_base.removeAllViews();
        //闪烁点关闭
        GlobalSetting.getInstance().icon_x = 0;
        GlobalSetting.getInstance().icon_y = 0.0f;
        GlobalSetting.getInstance().icon_r = 0.0f;

        stationMap=new StationMap3(context,datas,limit);
        FrameLayout.LayoutParams params=new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT);
        stationMap.setWidth_Height(fl_base.getWidth(),fl_base.getHeight());
        fl_base.addView(stationMap,params);
        stationMap.SetStationCur(position);

        effect = new StationEffect(context);
        fl_base.addView(effect);
    }

    private int position;
    public void setCurrenPosition(int positon){
        this.position=positon;
        if (stationMap!=null){
            stationMap.SetStationCur(position);
        }
        if (pages!=null&&pages.size()>0){
            isLooper=false;
            if (playHandler!=null&&playRunable!=null){
                playHandler.removeCallbacks(playRunable);
            }
            int tempSize=0;
            for (int i = 0; i < pages.size(); i++) {
                int size = pages.get(i).size();
                tempSize+=size;
                if (positon<size){
                    index=i;
                    break;
                }
            }
            isLooper=true;
            playHandler.postDelayed(playRunable,playTime);
        }
    }

    private class PlayRunable implements Runnable{
        @Override
        public void run() {
            if (!isLooper){
                return;
            }
            if (index>=pages.size()){
                index=0;
            }
            List<String[]> datas = pages.get(index);
            updateSubView(datas,maxCount);
            index++;
            playHandler.postDelayed(this,playTime);
        }
    }

    private void slipPageData(List<String[]> datas,int maxCount){
        if (datas==null||datas.size()<=0){
            return;
        }
        int slipCount=maxCount;
        if (datas.size()<maxCount){
            slipCount=datas.size();
        }
        List<String[]> data = datas.subList(0, slipCount);
        pages.add(data);
        if (slipCount>=datas.size()){
            return;
        }else {
            //递归处理
            List<String[]> subList = datas.subList(slipCount, datas.size());
            slipPageData(subList,maxCount);
        }
    }
}
